package com.rockbb.common.simpleframe.action;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.ArrayUtils;

import com.rockbb.common.simpleframe.config.SimpleConfig;

public abstract class GenericRequestBean
{
	/**
	 * 获取request中的字符串数组参数
	 * 
	 * @param key
	 * @param default_array
	 * @return
	 */
	public String[] getArray(String key, String[] default_array)
	{
		String[] array = this.getParameterValues(key);
		if (array == null || array.length == 0)
		{
			array = default_array;
		}
		return array;
	}

	public String[] getArray(String key, String[] default_array, String[] candidates)
	{
		String[] array = this.getParameterValues(key);
		if (array == null || array.length == 0)
		{
			array = default_array;
		}
		List<String> arrayList = Arrays.asList(array);
		Arrays.sort(candidates); //首先对数组排序
		for (int i=0; i<arrayList.size();)
		{
			if (Arrays.binarySearch(candidates, arrayList.get(i)) < 0)
				arrayList.remove(i);
			else
				i++;
		}
		return arrayList.toArray(new String[]{});
	}

	/**
	 * @param key
	 * @param default_array
	 * @param candidates can be null if there is no limit
	 * @return
	 */
	public int[] getIntArray(String key, int[] default_array, Integer[] candidates)
	{
		String[] array = this.getParameterValues(key);
		if (array == null || array.length == 0)
		{
			return default_array;
		}

		int[] ints = new int[array.length];
		int pos = 0;
		if (candidates != null)
		{
			Arrays.sort(candidates); //首先对数组排序
			for (int i=0; i<array.length;i++)
			{
				int value = 0;
				try { value = Integer.parseInt(array[i]); }
				catch (NumberFormatException e) {continue;}

				if (Arrays.binarySearch(candidates, value) >= 0)
				{
					ints[pos] = value;
					pos++;
				}
			}
		}
		else
		{
			for (int i=0; i<array.length;i++)
			{
				int value = 0;
				try { value = Integer.parseInt(array[i]); }
				catch (NumberFormatException e) {continue;}

				ints[pos] = value;
				pos++;
			}
		}

		return ArrayUtils.subarray(ints, 0, pos);
	}

	public int[] getIntArray(String key, int[] default_array, int min, int max)
	{
		String[] array = this.getParameterValues(key);
		if (array == null || array.length == 0)
		{
			return default_array;
		}
		int[] ints = new int[array.length];

		for (int i=0; i<array.length;i++)
		{
			int value = 0;
			try { value = Integer.parseInt(array[i]); }
			catch (NumberFormatException e) {continue;}

			if (value < min)
				ints[i] = min;
			else if (value > max)
				ints[i] = max;
			else
				ints[i] = value;
		}
		return ints;
	}

	/**
	 * 获取request中的浮点数参数
	 * 
	 * @param key
	 * @param default_value
	 * @return
	 */
	public float getFloat(String key, float default_value)
	{
		String str_value = this.getParameter(key);
		float value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Float.parseFloat(str_value.trim());
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的浮点数参数, 值必须在candidates中, 否则使用默认值
	 * 
	 * @param key
	 * @param default_value
	 * @param candidates
	 * @return
	 */
	public float getFloat(String key, float default_value, float[] candidates)
	{
		String str_value = this.getParameter(key);
		float value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Float.parseFloat(str_value.trim());
				Arrays.sort(candidates); //首先对数组排序
				if (Arrays.binarySearch(candidates, value) < 0) value = default_value;
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的浮点数参数, 值必须在min..max区间中, 否则使用默认值
	 * 
	 * @param key
	 * @param default_value
	 * @param min
	 * @param max
	 * @return
	 */
	public float getFloat(String key, float default_value, float min, float max)
	{
		String str_value = this.getParameter(key);
		float value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Float.parseFloat(str_value.trim());
				if (value < min || value > max) value = default_value;
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的整数参数, 默认为0
	 * 
	 * @param key
	 * @return
	 */
	public int getInt(String key)
	{
		return getInt(key, 0);
	}

	/**
	 * 获取request中的整数参数
	 * 
	 * @param key
	 * @param default_value
	 * @return
	 */
	public int getInt(String key, int default_value)
	{
		String str_value = this.getParameter(key);
		int value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Integer.parseInt(str_value.trim());
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的整数参数, 取值必须是candidates中的某一项
	 * 
	 * @param key
	 * @param default_value
	 * @param candidates
	 * @return
	 */
	public int getInt(String key, int default_value, int[] candidates)
	{
		String str_value = this.getParameter(key);
		int value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Integer.parseInt(str_value.trim());
				Arrays.sort(candidates);
				if (Arrays.binarySearch(candidates, value) < 0) value = default_value;
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的整数参数, 取值必须在min和max之间
	 * 
	 * @param key
	 * @param default_value
	 * @param min
	 * @param max
	 * @return
	 */
	public int getInt(String key, int default_value, int min, int max)
	{
		String str_value = this.getParameter(key);
		int value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Integer.parseInt(str_value.trim());
				if (value < min) value = min;
				else if (value > max) value = max;
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * 获取request中的长整数参数, 如果为空则使用默认值
	 * 
	 * @param key
	 * @param default_value
	 * @return
	 */
	public long getLong(String key, long default_value)
	{
		String str_value = this.getParameter(key);
		long value = default_value;
		if (str_value != null)
		{
			try
			{
				value = Long.parseLong(str_value.trim());
			}
			catch (Exception e)
			{
				value = default_value;
			}
		}
		return value;
	}

	/**
	 * @param key
	 * @param default_array
	 * @param candidates can be null if there is no limit
	 * @return
	 */
	public long[] getLongArray(String key, long[] default_array, long[] candidates)
	{
		String[] array = this.getParameterValues(key);
		if (array == null || array.length == 0)
		{
			return default_array;
		}

		long[] longs = new long[array.length];
		int pos = 0;
		if (candidates != null)
		{
			Arrays.sort(candidates); //首先对数组排序
			for (int i=0; i<array.length;i++)
			{
				long value = 0L;
				try { value = Long.parseLong(array[i]); }
				catch (NumberFormatException e) {continue;}

				if (Arrays.binarySearch(candidates, value) >= 0)
				{
					longs[pos] = value;
					pos++;
				}
			}
		}
		else
		{
			for (int i=0; i<array.length;i++)
			{
				long value = 0;
				try { value = Long.parseLong(array[i]); }
				catch (NumberFormatException e) {continue;}

				longs[pos] = value;
				pos++;
			}
		}

		return ArrayUtils.subarray(longs, 0, pos);
	}

	public String getUnicode(String key, String encoding)
	{
		return getUnicode(key, encoding, "");
	}

	/**
	 * 获取request中的字符串参数, 自动转换为指定的编码
	 * 
	 * @param key
	 * @param encoding
	 * @param default_value
	 * @return
	 */
	public String getUnicode(String key, String encoding, String default_value)
	{
		if (SimpleConfig.getConfig("server-encoding").equals(encoding))
		{
			return get(key, default_value);
		}

		try
		{
			byte[] tmp = get(key).getBytes(SimpleConfig.getConfig("server-encoding"));
			return new String(tmp, encoding);
		}
		catch(Exception e) 
		{
			return default_value;
		}
	}

	/**
	 * 获取request中的字符串参数, 默认为空字符串
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key)
	{
		return get(key, "");
	}

	/**
	 * 获取request中的字符串参数
	 * 
	 * @param key
	 * @param default_value
	 * @return
	 */
	public String get(String key, String default_value)
	{
		String value = this.getParameter(key);
		if (value == null)
		{
			value = default_value;
		}
		return value.trim();
	}

	/**
	 * 获取request中的字符串参数, 如果不在预设的candidates里面, 则返回默认值
	 * 
	 * @param key
	 * @param default_value
	 * @param candidates
	 * @return
	 */
	public String get(String key, String default_value, String[] candidates)
	{
		String value = this.getParameter(key);
		if (value == null)
		{
			value = default_value;
		}
		else
		{
			if (!Arrays.asList(candidates).contains(value)) value = default_value;
		}
		return value.trim();
	}

	public abstract HttpServletRequest getRequest();
	public abstract HttpServletResponse getResponse();
	protected abstract String[] getParameterValues(String key);
	protected abstract String getParameter(String key);
}
